/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.util.List;

/**
 * This class handles the shared logic for post type classes..
 *
 * @author Anton Strack
 */
public class PostManager {

    private Connection conn;
    private String requestView;
    protected String requestResultMessage;

    public PostManager(Connection conn) {
        this.conn = conn;
    }

    /**
     * This method is used to attach the recent user interaction history to the
     * request so it is available to the jsp pages
     *
     * @param request the request to add the user histories to.
     * @return the request object containing the user histories with key
     * "userHistories"
     */
    public CsdwaRequest attachUserRecentPosts(CsdwaRequest request, int postTypeId) {

        CsdwaDao<? extends Post> pdao = new DaoFactory(conn).getDao(postTypeId);
        InfoAggregator infoAgg = InfoAggregator.getInstance();
        String max = "LIMIT ";
        boolean properPost = true;
        if (postTypeId == infoAgg.getResourceTypeId("plot")) {
            max += Configuration.getRecentPlotMaxCount();// convert to string
        } else if (postTypeId == infoAgg.getResourceTypeId("character")) {
            max += Configuration.getRecentCharacterMaxCount();// convert to string
        } else if (postTypeId == infoAgg.getResourceTypeId("location")) {
            max += Configuration.getRecentLocationMaxCount();// convert to string
        } else if (postTypeId == infoAgg.getResourceTypeId("dialog")) {
            max += Configuration.getRecentDialogMaxCount();// convert to string
        } else {
            properPost = false;// if not a proper post just skip over the attach recent post section.
        }

        if (pdao != null && properPost == true) {
            pdao.setUseRelatedMarkers(true);
            pdao.addUsingQueryMarker(QueryMarker.LIMIT);
            pdao.setQueryMarkerValue(QueryMarker.LIMIT, max);
            pdao.selectBeans();
            /* might use the history visual markers on the sidebar items but not sure yet. */
//        UserHistoryManager<Plot> uhm = new UserHistoryManager<Plot>(CSDAppFactory.getDatasource());
//        List<Plot> plots = uhm.processUserPostHistory(pdao.getBeans(), req.getCurrentUser());

            request.setAttribute("recent" + InfoAggregator.getInstance().getResourceTypeName(postTypeId) + "s", pdao.getBeans());
        }
        return request;
    }

    /**
     * This method is used to obfuscate content text for guest users. It
     * primarily serves to prevent spoilers and revealing development stage
     * content for plots, characters, and dialogs, while still allowing guests
     * to explore the site. This method will obfuscate the post description and
     * post body only.
     *
     * @param request the request to potential obfuscate the posts of
     * @return the request object containing obfuscated posts if the user is a
     * guest.
     */
    public CsdwaRequest obfuscatePosts(CsdwaRequest request) {
        // only obfuscate posts if the user is a guest user.
        if (request.getCurrentUser().getRoleId() == InfoAggregator.getInstance().getRoleTypeId("guest")) {
            List<Post> posts = (List<Post>) request.getBeans();
            Post post = (Post) request.getBean();
            Obfuscator obfuscator = new Obfuscator();
            // ensure that the post is an instance of Post and thus we can call Post related methods
            if (posts != null && posts.isEmpty() == false && posts.get(0) instanceof Post) {
                for (Post p : posts) {
                    p.setDescription(obfuscator.obfuscate(p.getDescription()));
                }
                request.setBeans(posts);
            } else if (post != null && post instanceof Post) {
                post.setDescription(obfuscator.obfuscate(post.getDescription()));
                post.setBody(obfuscator.obfuscate(post.getBody()));
                request.setBean(post);
            }
        }
        return request;
    }
}
